<template>
	<view class="page">
		<uni-nav-bar background-color="#00a9ff" title="对话列表" color="white" :fixed="true"
		  statusBar="true"/>
		<uni-fab 
			ref="fab" 
			:pattern="pattern" 
			:horizontal="horizontal" 
			:vertical="vertical"
			@fabClick="newDialogue" />
		  

		<uni-list v-for="(item,index) in conversationList.slice().reverse()">
			<view :item="item"  @longpress="long(item)">
				<uni-list-chat title="知心AI" :avatarList="avatarURL" :time="item.time" 
				 style="background-color: #eaf8ff;" @click.native="goToConversation(item)"
				 :key="index"></uni-list-chat>				
			</view>

			
			
		</uni-list>

	</view>
</template>

<script>
	import date from '../../common/date.js'
	export default {
		data() {
			return {
				avatarURL: [
					{url:'/static/logo.png'}
				],
				conversationList: [
					//{time:'2024-5-22'}
				],
				
				horizontal: 'right',
				vertical: 'bottom',
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#007AFF',
					buttonColor: '#007AFF',
					iconColor: '#fff'
				},
			};
		},
		/*mounted() {
			// 与后端建立连接，获取对话列表数据
			this.fetchConversationList();
		},*/
		onShow() {
			this.conversationList= uni.getStorageSync('chats')||[];
		},
		methods: {
			/*fetchConversationList() {
				// 发起与后端的接口请求，获取对话列表数据
				// 示例代码：假设使用axios库发送HTTP请求
				axios.get('https://your-backend-api.com/conversations')
					.then(response => {
						this.conversationList = response.data;
					})
					.catch(error => {
						console.error('Failed to fetch conversation list', error);
					});
			},*/
			goToConversation(item) {
				//const conversationId = this.conversationList.indexOf(item);
				uni.navigateTo({
					//url: `/pages/dialogue?id=${conversationId}`
					url:`/pages/chatDetail/chatDetail?chattime=${item.time}`
				});
			},
			newDialogue(){
				const cur = date.getDate()+' '+date.getTime()
				let addlist = {time:cur}
				this.conversationList.push(addlist)
				uni.setStorageSync('chats',this.conversationList)
				
				uni.navigateTo({
					url:`/pages/dialogue/dialogue?chattime=${cur}`
				})
			},
			long(item){
				uni.showModal({
					title: '是否删除该对话记录？',
					confirmColor:'#007aff',
					cancelColor:'rgba(162, 140, 156, 0.7)',
					success: (res) =>{
						if (res.confirm) {
							const index = this.conversationList.indexOf(item);
							this.conversationList.splice(index,1);
							uni.setStorageSync('chats',this.conversationList);
						} else if (res.cancel) {
							return
						}
					}
				});
			},
		}
	};
</script>

<style>

</style>